package org.dromara.system.controller;

import java.util.Date;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.PayRateVo;
import org.dromara.system.domain.bo.PayRateBo;
import org.dromara.system.service.IPayRateService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 汇率
 * 前端访问路由地址为:/system/PayRate
 *
 * @author Gavino
 * @date 2024-10-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/PayRate")
public class PayRateController extends BaseController {

    private final IPayRateService payRateService;
    /**
     * 刷新汇率列表
     */
    @GetMapping("/refresh")
    public R<?> refresh() {
        return payRateService.refresh();
    }

    /**
     * 查询汇率列表
     */
    @GetMapping("/selectList")
    public R<?> selectList(PayRateBo bo) {
        List<PayRateVo> list = payRateService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 查询汇率列表
     */
    @SaCheckPermission("system:PayRate:list")
    @GetMapping("/list")
    public TableDataInfo<PayRateVo> list(PayRateBo bo, PageQuery pageQuery) {
        return payRateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出汇率列表
     */
    @SaCheckPermission("system:PayRate:export")
    @Log(title = "汇率", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayRateBo bo, HttpServletResponse response) {
        List<PayRateVo> list = payRateService.queryList(bo);
        ExcelUtil.exportExcel(list, "汇率", PayRateVo.class, response);
    }

    /**
     * 获取汇率刷新时间
     *
     */
    @GetMapping("/updateTime")
    public R<Date> updateTime() {
        return R.ok(payRateService.updateTime());
    }

    /**
     * 获取汇率详细信息
     *
     * @param rateId 主键
     */
    @SaCheckPermission("system:PayRate:query")
    @GetMapping("/{rateId}")
    public R<PayRateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long rateId) {
        return R.ok(payRateService.queryById(rateId));
    }

    /**
     * 新增汇率
     */
    @SaCheckPermission("system:PayRate:add")
    @Log(title = "汇率", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayRateBo bo) {
        return toAjax(payRateService.insertByBo(bo));
    }

    /**
     * 修改汇率
     */
    @SaCheckPermission("system:PayRate:edit")
    @Log(title = "汇率", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayRateBo bo) {
        return toAjax(payRateService.updateByBo(bo));
    }

    /**
     * 删除汇率
     *
     * @param rateIds 主键串
     */
    @SaCheckPermission("system:PayRate:remove")
    @Log(title = "汇率", businessType = BusinessType.DELETE)
    @DeleteMapping("/{rateIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] rateIds) {
        return toAjax(payRateService.deleteWithValidByIds(List.of(rateIds), true));
    }
}
